package com.brainydroid.example.rl;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

import com.brainydroid.framework.Object;
import com.brainydroid.framework.Scene;
import com.brainydroid.framework.Create;

import com.brainydroid.example.rl.Assets;
import com.brainydroid.example.rl.data2sd;

import com.brainydroid.items.Button;
import com.brainydroid.items.Text_box;


/*
Programming example : Numerical discrimination task
2 screens
- presentation of number
- feedback (correct/not correct)

The aim is to do something very similar to a psychtoolbox.
This means dealing with simple algorithmic (for, while, if)
and hiding the object structure to user



*/

public class ExampleRL extends Scene{

	
	//--------------------------------------------- Here Variables must be declared

	int i_sujet;
	String sd_root;

	Button b_cue_r;
	Button b_cue_l;
	Button b_fb;
	Button b_fixcross;
	
	
	
	Text_box tb_touchpoint;
	
	float screen_onset_time;
	float screen_display_time;

	int touched_button;
	int number;
	
	int N_trials_ = 200;

	int numbers[] = new int[N_trials_+1]; 
    float RT[] = new float[N_trials_+1]; // reaction times
	int A[]  = new int[N_trials_+1]; // actions
	float data[][] = new float[3][N_trials_+1];
	//--------------------------------------------- Here Variables must be declared

	
	/**
	 * Initialize the experiment. This is where objects should be specified and the spritebatch created
	 */
	public void init_experiments() {
				

	//	batcher_ = new SpriteBatch(); // init display
		
		// Setting experiments contingencies
		for(int j=0;j<N_trials_;j++){
			int k = 5;
			while (k==5){
					k = (int)(Math.random()*10);
			}
			numbers[j] = k;
		}
		
		
		Assets.load(); // loading assets for experimets
		Create.init(this); // Initializing item creation
		
		
		// Item creation 
		b_cue_l = Create.newButton(10f, 10f,   300f, 150f, 0);
		b_cue_r = Create.newButton(485f, 10f, 300f, 150f, 0);
		b_fb = Create.newButton(200f, 200f, 100f, 100f, 0);
		b_fixcross = Create.newButton(200f, 200f, 100f, 100f, 0);
		
		b_fixcross.center();
		b_fb.center();
		
		
		b_cue_l.setTxtreg(Assets.cue1);
		b_cue_r.setTxtreg(Assets.cue2);
		b_fb.setTxtreg(Assets.euro);
		b_fixcross.setTxtreg(Assets.fix_cross);
		
		tb_touchpoint = Create.newText_box(400, 400, "", 0);
				
		tb_touchpoint.setfont(Assets.font);
		
		
		
		// Initializing variables for first trial
		i_screen_ = 1;
		i_trial_ = 1;
		
		
		screen_onset_time = getTime();
		screen_display_time = 1;
		
		b_cue_r.hide();
		b_cue_l.hide();
		b_fb.hide();
		b_fixcross.show();

		tb_touchpoint.setText(sd_root);// "100,100";
		tb_touchpoint.hide();
		
		touched_button = 0;
		number = 0;
		
		number = numbers[i_trial_];
		
		setTime(0f);
		screen_onset_time = getTime();
		screen_display_time = 2;
	}

	
	public void experiment_loop(){
		
		if (i_trial_> N_trials_-1){
			try {
				end_of_exp();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}   
		   
		
		
		

		
		//
		// _______ first screen : presentation of the two symbols
		if (i_screen_ == 1){

			// manage transition
			if (getTime()> screen_onset_time+screen_display_time){
				i_screen_ = 2;
				screen_onset_time = getTime();
				screen_display_time = 2;
				b_cue_r.show();
				b_cue_l.show();
				b_fixcross.hide();
				
				i_trial_ += 1;
				
				number = numbers[i_trial_];

				
				}

			
		
		// _______ second screen : feedback presentation	
		}else if(i_screen_ == 2){
		
			// manage touch event
			if (b_cue_l.checktouched()){
				b_cue_r.hide(); 
				touched_button = 1;
				RT[i_trial_-1] = getTime() - screen_onset_time;
				A[i_trial_-1] = 1;
				}
			if (b_cue_r.checktouched()){
				b_cue_l.hide();  touched_button = 2;
				RT[i_trial_-1] = getTime() - screen_onset_time;
				A[i_trial_-1] = 2;
				}
			
			// manage transitions
			if ((getTime()> screen_onset_time+screen_display_time)){
				i_screen_ = 3;
				screen_onset_time = getTime();
				screen_display_time = 2;
				b_cue_r.hide();
				b_cue_l.hide();
				b_fb.show();
				
				
				b_fb.changeTxtreg(Assets.wrong);

				if ((number<5) & (touched_button == 1)){
					b_fb.changeTxtreg(Assets.euro);
				}

				if ((number>5) & (touched_button == 2)){
					b_fb.changeTxtreg(Assets.euro);
				}
				
				
			}
						
		}else if(i_screen_ == 3){
			
			// manage transition
			if (getTime()> screen_onset_time+screen_display_time){
				i_screen_ = 1;
				screen_onset_time = getTime();
				screen_display_time = 2;
				b_fb.hide();
				b_fixcross.show();
				i_trial_ += 1;
				
				number = numbers[i_trial_];

				
				}
			
		}
	}

  

	
	@Override
	public void create() {
		// oncreate();
		init_experiments();
		// TODO Auto-generated method stub
	}
	
	
	public ExampleRL(int i) {
		// TODO Auto-generated constructor stub
		i_sujet = i;
	}
	



	public void end_of_exp() throws IOException{

	// filling data
		for (int i = 0 ; i<N_trials_; i++){
			data[0][i] = (float)numbers[i];
			data[1][i] = (float)RT[i];
			data[2][i] = (float)A[i];
		}
		
		String[] head = new String[3]; 
		head[0]=new String ("number"); 
		head[1]=new String ("delay"); 
		head[2]=new String ("answer"); 
		
	//	data2sd.storedata(head,data);
		dispose();

		

	}



	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		
	}

	
	

}
